package com.pcbly.web.persistence.field;

import java.util.List;

import org.hibernate.Session;
import org.hibernate.Transaction;

import com.pcbly.web.bean.field.FieldSettingBean;
import com.pcbly.web.bean.user.UserBean;
import com.pcbly.web.exception.CrmException;
import com.pcbly.web.persistence.HibernateBaseDao;

public class FieldSettingHibernateDao extends
		HibernateBaseDao<FieldSettingBean> implements FieldSettingDao {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1866061754299565115L;
	public FieldSettingHibernateDao(){
		super();
	}
	protected Class<FieldSettingBean> getEntityClass() {

		return FieldSettingBean.class;
	}

	@SuppressWarnings("unchecked")
	public List<FieldSettingBean> queryListByTableName(String tableName)
			throws Exception {
		if (tableName == null || "".equals(tableName.trim())) {
			throw new CrmException(" tablename is null ");
		}

		List<FieldSettingBean> list = (List<FieldSettingBean>) getHibernateTemplate()
				.find("from FieldSettingBean where tableName=?",
						new Object[] { tableName });
		return list;
	}

	@SuppressWarnings("unchecked")
	public List<FieldSettingBean> queryListByTableNameAndUserBean(
			String tableName, UserBean user) throws Exception {
		if (tableName == null || "".equals(tableName.trim())) {
			throw new CrmException(" tablename is null ");
		}

		List<FieldSettingBean> list = (List<FieldSettingBean>) getHibernateTemplate()
				.find(
						"from FieldSettingBean where tableName=? and userBean=? ",
						new Object[] { tableName, user });
		return list;
	}

	public void createList(List<FieldSettingBean> fieldBeans) {
		Session se = getSession();
		Transaction tran = se.beginTransaction();

		for (FieldSettingBean bean : fieldBeans)
			getSession().save(bean);
		tran.commit();
	}

	public void updateList(List<FieldSettingBean> fieldBeans) {
		Session se = getSession();
		Transaction tran = se.beginTransaction();

		for (FieldSettingBean bean : fieldBeans)
			getSession().update(bean);
		tran.commit();
	}

	public void deleteList(List<FieldSettingBean> fieldBeans) {
		Session se = getSession();
		Transaction tran = se.beginTransaction();

		for (FieldSettingBean bean : fieldBeans)
			getSession().delete(bean);
		tran.commit();
	}
}
